<?php

/**
 * Laravel-admin - admin builder based on Laravel.
 * @author z-song <https://github.com/z-song>
 *
 * Bootstraper for Admin.
 *
 * Here you can remove builtin form field:
 * Encore\Admin\Form::forget(['map', 'editor']);
 *
 * Or extend custom form field:
 * Encore\Admin\Form::extend('php', PHPEditor::class);
 *
 * Or require js and css assets:
 * Admin::css('/packages/prettydocs/css/styles.css');
 * Admin::js('/packages/prettydocs/js/main.js');
 *
 */

\Encore\Admin\Form::forget(['editor','map','tags']);

\Encore\Admin\Form::extend('uploader',\App\Admin\Extensions\Uploader::class);
\Encore\Admin\Form::extend('tags',\App\Admin\Extensions\Form\Tag::class);
\Encore\Admin\Form::extend('ckeditor', \App\Admin\Extensions\Form\CKEditor::class);
\Encore\Admin\Form::extend('contact',\App\Admin\Extensions\Form\Contact::class);
\Encore\Admin\Grid::init(function (\Encore\Admin\Grid $grid) {
    $grid->actions(function (\Encore\Admin\Grid\Displayers\Actions $actions) {
        $actions->disableView();

    });
});

if (!function_exists('ignore_pjax_paths')) {
    /**
     * 排除不使用 Pjax 刷新页面的后台路径
     *
     * @param  array  $freshPaths
     * @return void
     */
    function ignore_pjax_paths($freshPaths) {
        $prefix = config('admin.route.prefix');

        $freshPathsJson = collect($freshPaths)->map(function ($path) use ($prefix) {
            return "/\/$prefix\/$path/";
        })->implode(',');
        $freshPathsJson = "[$freshPathsJson]";

        Admin::script(<<<EOT
$(document).on('pjax:start', function (e, contents, options) {
    const freshPaths = $freshPathsJson;
    for (let path of freshPaths) {
        if (options.url.search(path) !== -1) {
            location.reload();
            return false;
        }
    }
});
EOT
        );
    }
}
ignore_pjax_paths([
    'workshop\/.*',
    'news\/.*',
    'park\/.*',
]);

//Admin::disablePjax();
app('view')->prependNamespace('admin', resource_path('views/admin'));

